﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Interface_Tier;
using System.Configuration;
using Transfer_Object_Tier;

namespace eSolution_HRMSS.Winforms.DanhMuc
{
    public partial class FR_ToNhom : DevExpress.XtraEditors.XtraForm
    {
        string ma_hoc_van = "";
        string new_id = "";
        bool isUpdate = false;
        IHRM_GROUPData _IHRM_GROUPData;
        IHRM_BRANCHData _IHRM_BRANCHData;
        IHRM_DEPARTMENTData _IHRM_DEPARTMENTData;

        ICommonCode _icommon;
        eSolution_HRMSS.HRMSS_Ultil util = new HRMSS_Ultil();
        public delegate void updatelist();
        public updatelist truyentinhieu;


        public FR_ToNhom(bool _isUpdate)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
        }

        public FR_ToNhom(bool _isUpdate, string mahocvan)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
            ma_hoc_van = mahocvan;
        }

        private void FR_HocVan_Load(object sender, EventArgs e)
        {
            _IHRM_GROUPData = ( IHRM_GROUPData)Activator.GetObject(typeof( IHRM_GROUPData), util.ChuoiBT_HRM_GROUPData);
            _icommon = (ICommonCode)Activator.GetObject(typeof(ICommonCode), util.Chuoicommon);
            _IHRM_BRANCHData = (IHRM_BRANCHData)Activator.GetObject(typeof(IHRM_BRANCHData), util.Chuoichinhanh);
            _IHRM_DEPARTMENTData = (IHRM_DEPARTMENTData)Activator.GetObject(typeof(IHRM_DEPARTMENTData), util.Chuoiphongban);
            
            tangid();
            load_ds_chi_nhanh();
            if (isUpdate)
            {
                //load thong tin update
                loadthongtin();
            }
            else
            {
                txtma.Text = new_id;
            }
        }

        private void tangid()
        {
            new_id = _icommon.Generate_ID("HRM_GROUP", "GroupCode", "TN");
        }
        private void load_ds_chi_nhanh()
        {
            lu_chinhanh.Properties.DataSource = _IHRM_BRANCHData.HRM_BRANCH_GetList().Tables[0];
            lu_chinhanh.Properties.DisplayMember = "BranchName";
            lu_chinhanh.Properties.ValueMember = "BranchCode";
        }
        private void load_ds_phong_ban(string machinhanh)
        {
            lu_phongban.Properties.DataSource = _IHRM_DEPARTMENTData.HRM_DEPARTMENT_GetListByBranchCode(machinhanh).Tables[0];
            lu_phongban.Properties.DisplayMember = "DepartmentName";
            lu_phongban.Properties.ValueMember = "DepartmentCode";
        }
        private void loadthongtin()
        {
            Laythongtin(ma_hoc_van);
        }

        private void Laythongtin(string educode)
        {
            DataSet ds = _IHRM_GROUPData.HRM_GROUP_Get(educode);

            if (ds.Tables.Count>0)
            {
                string ma = ds.Tables[0].Rows[0][0].ToString();
                string ten = ds.Tables[0].Rows[0][2].ToString();
                string slthuc = ds.Tables[0].Rows[0][4].ToString();
                string slao = ds.Tables[0].Rows[0][3].ToString();
                string ghichu = ds.Tables[0].Rows[0][5].ToString();
                string brandcode = ds.Tables[0].Rows[0][7].ToString();
                string DepartmentCode = ds.Tables[0].Rows[0][1].ToString();

                txtma.Text = ma;
                txtten.Text = ten;
                txtslao.Text= slthuc;
                txtslthuc.Text = slao;
                txtghichu.Text = ghichu;
                lu_chinhanh.EditValue = brandcode;
                lu_phongban.EditValue = DepartmentCode;
            }

        }

        private int Hoc_van_insert()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            string phone = txtten.Text;
            int slthuc = int.Parse(txtslthuc.Text);
            int slao =int.Parse( txtslao.Text);
            string ghichu = txtghichu.Text;
            string maphongban = lu_phongban.EditValue.ToString();

            int kq = _IHRM_GROUPData.HRM_GROUP_Insert(ma, maphongban, ten, slao, slthuc, ghichu);
            return kq;
        }

        private int Hoc_van_update()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            string phone = txtten.Text;
            int slthuc = int.Parse(txtslthuc.Text);
            int slao = int.Parse(txtslao.Text);
            string ghichu = txtghichu.Text;
            string maphongban = lu_phongban.EditValue.ToString();

            int kq = _IHRM_GROUPData.HRM_GROUP_Update(ma, maphongban, ten, slao, slthuc, ghichu);
            return kq;
        }

        private void btluudong_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;

            if (!isUpdate )
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private bool validate_data()
        {
            if (txtma.Text.Length == 0 || txtten.Text.Length == 0 || lu_chinhanh.EditValue == null || lu_phongban.EditValue == null)
            {
                MessageBox.Show(util.Loi_thieuthongtin);
                return false;
            }
            else
                return true;
        }

        private void clear_form()
        {
            tangid();
            txtma.Text = new_id;
            txtten.Text = "";
            txtten.Text = "";
            lu_chinhanh.EditValue = null;
            lu_phongban.EditValue = null;
            txtslao.Text = "0";
            txtslthuc.Text = "0";
            txtghichu.Text = "";
            isUpdate = false;
        }

        private void btluuthem_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;
            if (!isUpdate)
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private void btdong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lu_chinhanh_EditValueChanged(object sender, EventArgs e)
        {
            if (lu_chinhanh.EditValue == null)
            {
                return;
            }
            string BranchCode = lu_chinhanh.EditValue.ToString();
            if (BranchCode != "")
            {
                load_ds_phong_ban(BranchCode);
            }
        }
    }
}